package com.xq.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xq.shop.config.Redis;
import com.xq.shop.dao.domain.entity.RestCalendar;
import com.xq.shop.dao.mapper.RestCalendarMapper;
import com.xq.shop.dto.EditRestCalendarDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.service.IRestCalendarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.vo.RestCalendarVo;
import enums.DataStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.BasePage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 休息日历表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-19
 */
@Service
public class RestCalendarServiceImpl extends ServiceImpl<RestCalendarMapper, RestCalendar> implements IRestCalendarService {

    @Resource
    private Redis redis;

    @Override
    public BasePage<RestCalendarVo> pageQuery(QueryDto dto) {
        LambdaQueryWrapper<RestCalendar> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RestCalendar::getStatus, DataStatus.NORMAL.getCode());
        wrapper.eq(RestCalendar::getType,2);
        wrapper.orderByDesc(RestCalendar::getId);
        IPage<RestCalendar> page = page(new Page<>(dto.getPage(), dto.getLimit()), wrapper);
        List<RestCalendar> restCalendarList = page.getRecords();
        if (restCalendarList.isEmpty()) {
            return new BasePage<RestCalendarVo>().initPage(dto.getPage(), dto.getLimit(), page.getTotal(), page.getPages(), new ArrayList<>());
        }
        List<RestCalendarVo> voList = beanToVo(restCalendarList);
        return new BasePage<RestCalendarVo>().initPage(dto.getPage(), dto.getLimit(), page.getTotal(), page.getPages(), voList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(EditRestCalendarDto dto, Integer userId) {
        RestCalendar restCalendar;
        if (dto.getType() == 1) {
            LambdaQueryWrapper<RestCalendar> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RestCalendar::getStatus, DataStatus.NORMAL.getCode());
            wrapper.eq(RestCalendar::getType, 1);
            restCalendar = getOne(wrapper);
            if (Objects.nonNull(restCalendar)) {
                BeanUtils.copyProperties(dto, restCalendar);
                restCalendar.setUpdateUser(userId);
                updateById(restCalendar);
            } else {
                restCalendar = new RestCalendar();
                BeanUtils.copyProperties(dto, restCalendar);
                restCalendar.setCreateUser(userId);
                save(restCalendar);
            }
        } else {
            restCalendar = new RestCalendar();
            BeanUtils.copyProperties(dto, restCalendar);
            restCalendar.setCreateUser(userId);
            save(restCalendar);
        }
        redis.remove("restCalendar");

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeRestCalendar(IntegerDto dto, Integer id) {
        RestCalendar restCalendar = getById(dto.getValue());
        if (Objects.nonNull(restCalendar)) {
            restCalendar.setStatus(DataStatus.INVALID.getCode());
            restCalendar.setUpdateUser(id);
            updateById(restCalendar);
        }
        redis.remove("restCalendar");
    }

    @Override
    public RestCalendarVo getRegular(){
        LambdaQueryWrapper<RestCalendar> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RestCalendar::getStatus, DataStatus.NORMAL.getCode());
        wrapper.eq(RestCalendar::getType,1);
        RestCalendar restCalendar = getOne(wrapper);
        RestCalendarVo vo = new RestCalendarVo();
        BeanUtils.copyProperties(restCalendar, vo);
        return vo;
    }

    private List<RestCalendarVo> beanToVo(List<RestCalendar> restCalendarList) {
        List<RestCalendarVo> voList = new ArrayList<>();
        restCalendarList.forEach(restCalendar -> {
            RestCalendarVo vo = new RestCalendarVo();
            BeanUtils.copyProperties(restCalendar, vo);
            voList.add(vo);
        });
        return voList;
    }
}
